﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Data;


public partial class FillFeedbackUI : System.Web.UI.Page
{
    private int feedback_id;

    protected void Page_Load(object sender, EventArgs e)
    {

        Master.FindControl("MainMenuStudent").Visible = true;

        if ((String)Session["Username"] == null || (String)Session["Username"] == "admin" || (String)Session["Feedback_ID"] == null)
        {
            Response.Redirect("~/LoginStudent.aspx");
        }

        if (Page.IsPostBack == false)
        {
            feedback_id = Int32.Parse((String)Session["Feedback_ID"]);
            String sql = "Select Course_ID, Course_Name from course_feedback WHERE Feedback_ID=" + feedback_id;
            DataSet dataSet1 = new DataSet();
            DbUtilities dbUtil = new DbUtilities(sql);
            dataSet1 = dbUtil.retrieveInfo();
            courseIDLabel.Text = (String)dataSet1.Tables[0].Rows[0].ItemArray[0];
            courseNameLabel.Text = (String)dataSet1.Tables[0].Rows[0].ItemArray[1];

            sql = "Select Instructor_Name from instructor WHERE Feedback_ID=" + feedback_id;
            dbUtil = new DbUtilities(sql);
            dataSet1 = dbUtil.retrieveInfo();
            courseInstructorLabel.Text = (String)dataSet1.Tables[0].Rows[0].ItemArray[0];

            sql = "Select TA_Name from ta WHERE Feedback_ID=" + feedback_id;
            dbUtil = new DbUtilities(sql);
            dataSet1 = dbUtil.retrieveInfo();
            for (int i = 0; i < dataSet1.Tables[0].Rows.Count; i++)
            {
                courseTALabel.Text += (String)dataSet1.Tables[0].Rows[i].ItemArray[0];
                if (i != dataSet1.Tables[0].Rows.Count - 1)
                    courseTALabel.Text += ", ";
            }
        }


    }

    public Boolean validateResponse()
    {
        if (questionCourse1List.SelectedIndex == -1 ||
            questionCourse2List.SelectedIndex == -1 ||
            questionCourse3List.SelectedIndex == -1 ||
            questionCourse4List.SelectedIndex == -1 ||
            questionCourse5List.SelectedIndex == -1 ||
            questionInstruction1List.SelectedIndex == -1 ||
            questionInstruction2List.SelectedIndex == -1 ||
            questionInstruction3List.SelectedIndex == -1 ||
            questionInstruction4List.SelectedIndex == -1 ||
            questionTutLab1List.SelectedIndex == -1)
        {
            Response.Write("<Script language=javascript>alert('Fill the options for all the questions!!')</Script>");
            return false;
        }

        return true;
    }

    protected void initiateFillFeedback(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            if (validateResponse())
            {
                FeedbackResponse response = new FeedbackResponse();
                
                feedback_id = Int32.Parse((String)Session["Feedback_ID"]);
                response.setFeedbackID(feedback_id);
                response.setCourseResponses(questionCourse1List.SelectedItem.Value.ToString(), questionCourse2List.SelectedItem.Value.ToString(), questionCourse3List.SelectedItem.Value.ToString(), questionCourse4List.SelectedItem.Value.ToString(), questionCourse5List.SelectedItem.Value.ToString());
                response.setInstructorResponses(questionInstruction1List.SelectedItem.Value.ToString(),questionInstruction2List.SelectedItem.Value.ToString(),questionInstruction3List.SelectedItem.Value.ToString(),questionInstruction4List.SelectedItem.Value.ToString());
                response.setTutLabResponse(questionTutLab1List.SelectedItem.Value.ToString());
                response.setTAOpenResponse(TAOpenTextBox.Text);
                response.setOpenResponses(Open1TextBox.Text,Open2TextBox.Text,Open3TextBox.Text);
                String Student_ID = (String)Session["Username"];
                int flag = response.saveResponse(Student_ID);
                if (flag==2)
                {
                    Response.Write("<Script language=javascript>alert('Some Error Occured!!')</Script>");
                    return;
                }
                Response.Write("<Script language=javascript>alert('Successfully Filled!!')</Script>");
                Response.Redirect("~/Student/WelcomeStudentUI.aspx");
            }
        }
    }



}